package com.pearadmin.system.controller;

import com.github.pagehelper.PageInfo;
import com.pearadmin.system.domain.SysMotor;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.pearadmin.system.service.ISysMotorService;

/**
 * 电机监控Controller
 * 
 * @author Ryan
 * @date 2021-03-20
 */
@RestController
@RequestMapping("/system/motor")
public class SysMotorController extends BaseController
{
    private String prefix = "system/motor";

    @Autowired
    private ISysMotorService sysMotorService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/system/motor/main','system:motor:main')")
    public ModelAndView main()
    {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询电机监控列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/system/motor/data','system:motor:data')")
    public ResultTable list(@ModelAttribute SysMotor sysMotor, PageDomain pageDomain)
    {
        PageInfo<SysMotor> pageInfo = sysMotorService.selectSysMotorPage(sysMotor,pageDomain);
        return pageTable(pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 新增电机监控
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/system/motor/add','system:motor:add')")
    public ModelAndView add()
    {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存电机监控
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/system/motor/add','system:motor:add')")
    public Result save(@RequestBody SysMotor sysMotor)
    {
        return decide(sysMotorService.insertSysMotor(sysMotor));
    }

    /**
     * 修改电机监控
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/system/motor/edit','system:motor:edit')")
    public ModelAndView edit(Long motorId, ModelMap mmap)
    {
        SysMotor sysMotor = sysMotorService.selectSysMotorById(motorId);
        mmap.put("sysMotor", sysMotor);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存电机监控
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/system/motor/edit','system:motor:edit')")
    public Result update(@RequestBody SysMotor sysMotor)
    {
        return decide(sysMotorService.updateSysMotor(sysMotor));
    }

    /**
     * 删除电机监控
     */
    @ResponseBody
    @DeleteMapping( "/batchRemove")
    @PreAuthorize("hasPermission('/system/motor/remove','system:motor:remove')")
    public Result batchRemove(String ids)
    {
        return decide(sysMotorService.deleteSysMotorByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/remove/{motorId}")
    @PreAuthorize("hasPermission('/system/motor/remove','system:motor:remove')")
    public Result remove(@PathVariable("motorId") Long motorId)
    {
        return decide(sysMotorService.deleteSysMotorById(motorId));
    }
}
